from common.unit import AndroidUI
from common.home_page import HomePageObject
from common.qr_code import QRCodeX
from object.lnitial.lnitial_page import LnitialObject
from object.storage.storage_page import StorageObject
from common.request_result import GetCode
import time


#科室装袋，入库操作
class StorageTest:
    def __init__(self):
        self.unit = AndroidUI
        self.home = HomePageObject()
        self.qrcode = QRCodeX()
        self.ln = LnitialObject()
        self.st = StorageObject()
        self.get_code = GetCode()
        self.driver = AndroidUI.get_webdriver()

    # 返回
    def back_button(self):
        self.driver.keyevent(4)

    # 清理人登录
    def check_login(self):
        # self.unit.scan(self.qrcode.zhaoqingli)
        try:
            self.ln.system_button()
            self.unit.scan(self.qrcode.zhaoqingli)
            print("登录成功")
        except:
            print("已经登录")

    # 点击入库按钮，进入入库界面，并且返回首页
    def enter_storage_click(self):
        try:
            self.home.enter_button().click()
            self.st.lib_check_bag_button()
            print("点击入库按钮进入入库界面成功")
            AndroidUI().write_result("点击入库按钮进入入库界面", "成功")
            self.back_button()
        except:
            print("点击入库按钮进入入库界面失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击入库按钮进入入库界面", "失败")
            # self.driver.quit()

    # 扫码进入入库界面
    def enter_storage_scan(self):
        try:
            self.unit.scan(self.qrcode.zancunjian)
            self.st.lib_check_bag_button()
            print("扫码进入入库界面成功")
            AndroidUI().write_result("扫码进入入库界面", "成功")

        except:
            print("扫码进入入库界面失败")
            AndroidUI.capture()
            AndroidUI().write_result("扫码进入入库界面", "失败")

    # 点击装箱按钮进入装箱界面
    def enter_box_page(self):
        try:
            self.st.lib_packing_button().click()
            self.st.listbox_button()
            print("点击装箱按钮进入装箱界面成功")
            AndroidUI().write_result("点击装箱按钮进入装箱界面", "成功")
        except:
            print("点击装箱按钮进入装箱界面失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击装箱按钮进入装箱界面", "失败")

    # 扫描垃圾袋二维码
    def scan_bag(self):
        try:
            self.unit.scan(self.get_code.get_bag_cod(0))
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
            # self.unit.scan(self.get_code.get_bag_cod(2))
            # self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO.2")')
            # self.unit.scan(self.get_code.get_bag_cod(1))
            # self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO.3")')
            print("扫码垃圾袋成功")
            AndroidUI().write_result("扫描垃圾袋", "成功")
            self.back_button()
        except:
            print("扫码垃圾袋失败")
            AndroidUI.capture()
            AndroidUI().write_result("扫码垃圾袋", "失败")

    # 返回首页
    def back_home(self):
        try:
            self.st.back_button().click()
            self.st.back_button().click()
            self.home.home_page()
            print("返回首页成功")
            AndroidUI().write_result("返回首页", "成功")
        except:
            print("返回首页失败")
            self.back_button()
            AndroidUI.capture()
            AndroidUI().write_result("返回首页界面", "失败")

    def start_test(self):
        self.check_login()
        self.enter_storage_click()
        self.enter_storage_scan()
        self.enter_box_page()
        self.scan_bag()
        self.back_home()
        # self.back_button()


if __name__ == '__main__':
    st = StorageTest()

    st.start_test()
